﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;
using DACNPM.MyUserControl.UCHienThi;

namespace DACNPM.MyUserControl.UCHienThi
{
    public partial class UCDanhSachDDH : UserControl
    {
        DonDatHangBUO ddhBUO = new DonDatHangBUO();
        int id;
        String ddh;
        String idnhanvien;
        String NCC;

        public UCDanhSachDDH(String idtoancuc)
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ddhBUO.HienThiDanhSachDDH();
            this.idnhanvien = idtoancuc;
        }

        private void rdTimTheoMaDDH_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoMa.Text = "";
            txtTimTheoMa.Enabled = true;
            txtTimTheoTenNV.Text = "";
            txtTimTheoTenNV.Enabled = false;
            dateTimePicker1.Enabled = false;
            dataGridView1.Visible = true;
            dataGridView1.DataSource = ddhBUO.HienThiDanhSachDDH();
            dataGridView1.AutoGenerateColumns = false;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (rdTimTheoMaDDH.Checked == true)
            {
                if (txtTimTheoMa.Text == "")
                {
                    MessageBox.Show("Chưa nhập mã đơn đặt hàng cần tìm!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    dataGridView1.DataSource = ddhBUO.searchByMaDDH(txtTimTheoMa.Text);
                }
            }
            else if(rdTimNV.Checked == true)
            {
                if (txtTimTheoTenNV.Text == "")
                {
                    MessageBox.Show("Chưa nhập tên nhân viên của đơn đặt hàng cần tìm!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    dataGridView1.DataSource = ddhBUO.searchByTenNV(txtTimTheoTenNV.Text);
                }
            }
        }

        private void rdTimNV_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoMa.Text = "";
            txtTimTheoMa.Enabled = false;
            txtTimTheoTenNV.Text = "";
            txtTimTheoTenNV.Enabled = true;
            dateTimePicker1.Enabled = false;
            dataGridView1.Visible = true;
            dataGridView1.DataSource = ddhBUO.HienThiDanhSachDDH();
            dataGridView1.AutoGenerateColumns = false;
        }

        private void rdNgayDatHang_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoMa.Text = "";
            txtTimTheoMa.Enabled = false;
            txtTimTheoTenNV.Text = "";
            txtTimTheoTenNV.Enabled = false;
            dateTimePicker1.Enabled = true;
            dataGridView1.Visible = true;
            dataGridView1.DataSource = ddhBUO.HienThiDanhSachDDH();
            dataGridView1.AutoGenerateColumns = false;
        }

        private void txtTimTheoMa_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = ddhBUO.searchByMaDDH(txtTimTheoMa.Text);
            dataGridView1.AutoGenerateColumns = false;
        }

        private void txtTimTheoTenNV_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = ddhBUO.searchByTenNV(txtTimTheoTenNV.Text);
            dataGridView1.AutoGenerateColumns = false;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = ddhBUO.searchByNDH(DateTime.Parse(dateTimePicker1.Text));
            dataGridView1.AutoGenerateColumns = false;
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));

                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        ddh = dataGridView1.Rows[id].Cells[0].Value.ToString();
                        NCC = dataGridView1.Rows[id].Cells[1].Value.ToString();


                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void thêmĐơnĐặtHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCThemDDH ucThemDDH = new UCThemDDH(idnhanvien);
            Form form = this.ParentForm;

            ControlAccess.ShowConTrol(form, ucThemDDH, "grbHienThiChucNang");
        }

        private void sửaĐơnĐặtHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCSuaDDH ucSuaDDH = new UCSuaDDH(ddh, idnhanvien, NCC);
            Form form = this.ParentForm;

            ControlAccess.ShowConTrol(form, ucSuaDDH, "grbHienThiChucNang");
        }

        private void xóaĐơnĐặtHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn là muốn xóa hay không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (ddhBUO.Delete(ddh))
                {
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = ddhBUO.HienThiDanhSachDDH();
                    MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
    }
}
